﻿using System;
using System.Windows;
using DSPL.Services;
using DSPL.Contracts;
using DSPL.Common;
using System.Data;
using DSPL.Services.Services;
using DSPL.Contracts.Common;
using DSPL.Contracts.ViewModel;
using DSPL.Contracts.Command;
using DSPL.Finac.Common;
using System.Collections.Generic;
using System.Windows.Media;
using DSPL.Common.CommonControls.Controls;
using System.Windows.Controls;

namespace DSPL.Finac.ViewModel
{
    public class TaxMasterViewModel : BaseViewModel
    {
        private bool isTaxCodeFocused;
        private bool isTaxNameFocused;
        private bool isReceivableAcFocused;
        private bool isPayableAcFocused;
        private bool isTaxPercentFocused;
        private Dictionary<string, string> _bindReceivableAc = new Dictionary<string, string>();
        private Dictionary<string, string> _bindPayableAc= new Dictionary<string, string>();


        #region Define properties

        public bool IsTaxCodeFocused
        {
            get { return isTaxCodeFocused; }
            set
            {
                isTaxCodeFocused = value;
                this.OnPropertyChanged("IsTaxCodeFocused");
            }
        }

        public bool IsTaxNameFocused
        {
            get { return isTaxNameFocused; }
            set
            {
                isTaxNameFocused = value;
                this.OnPropertyChanged("IsTaxNameFocused");

            }
        }

        public bool IsReceivableAcFocused
        {
            get { return isReceivableAcFocused; }
            set
            {
                isReceivableAcFocused = value;
                this.OnPropertyChanged("IsReceivableAcFocused");

            }
        }

        public bool IsPayableAcFocused
        {
            get { return isPayableAcFocused; }
            set
            {
                isPayableAcFocused = value;
                this.OnPropertyChanged("IsPayableAcFocused");

            }
        }

        public bool IsTaxPercentFocused
        {
            get { return isTaxPercentFocused; }
            set
            {
                isTaxPercentFocused = value;
                this.OnPropertyChanged("IsTaxPercentFocused");

            }
        }

        public Dictionary<string, string> BindReceivableAc
        {
            get { return _bindReceivableAc; }
            set
            {
                _bindReceivableAc = value;
                this.OnPropertyChanged("BindReceivableAc");

            }
        }

        public Dictionary<string, string> BindPayableAc
        {
            get { return _bindPayableAc; }
            set
            {
                _bindPayableAc = value;
                this.OnPropertyChanged("BindPayableAc");

            }
        }

        #endregion


        #region Constructor

        public TaxMasterViewModel()
            : base()
        {
            MessageTitle = "Tax Master";
        }

        #endregion
        /// <summary>
        /// Validate mandatory data before save
        /// </summary>
        /// <param name="obj"></param>
        protected override void PreDataValidateExecuted(object obj)
        {
            IsTaxCodeFocused = false;
            IsTaxNameFocused = false;
            IsReceivableAcFocused = false;
            IsPayableAcFocused = false;
            IsTaxPercentFocused = false;

            var messageService = new MessageService();
            if (CurrentRow["TaxCode"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.TaxCodeNotEmpty, MessageTitle, MessageType.InfoOK);
                IsTaxCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["TaxName"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.TaxNameNotEmpty, MessageTitle, MessageType.InfoOK);
                IsTaxNameFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["TaxRecAcc"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.ReceivableAcNotEmpty, MessageTitle, MessageType.InfoOK);
                IsReceivableAcFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["TaxPayAcc"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.PayableAcNotEmpty, MessageTitle, MessageType.InfoOK);
                IsPayableAcFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["TaxPer"]==DBNull.Value || Convert.ToInt32(CurrentRow["TaxPer"]) == 0)
            {
                messageService.ShowMessage(MessageText.TaxPercentNotEmpty, MessageTitle, MessageType.InfoOK);
                IsTaxPercentFocused = true;
                this.IsValid = false;
                return;
            }

            this.IsValid = true;
        }

        protected override void SetDataSourcesAndProperties()
        {
            BindReceivableAc.Add("AccDesc", "txtReceivableTitle");
            BindPayableAc.Add("AccDesc", "txtPayableTitle");

        }


        protected override void RefreshExternalDataObjectsExecueted(object obj)
        {

            base.RefreshExternalDataObjectsExecueted(obj);
            DisplayDataInControls(obj);           

        }

        private void DisplayDataInControls(object obj)
        {
            if (obj == null)
            {
                return;
            }
            Visual rootContainer = obj as Visual;
            if (rootContainer != null)
            {
                int ChildNumber = VisualTreeHelper.GetChildrenCount(rootContainer);

                for (int i = 0; i < ChildNumber; i++)
                {
                    Visual childVisual = (Visual)VisualTreeHelper.GetChild(rootContainer, i);

                    if ((childVisual.GetType() == typeof(TabControl) || (childVisual.GetType() == typeof(Grid)) || (childVisual.GetType() == typeof(Border))) && (VisualTreeHelper.GetChildrenCount(childVisual) > 0))
                    {
                        if (childVisual.GetType() == typeof(TabControl))
                        {
                            foreach (TabItem tb in ((TabControl)childVisual).Items)
                            {
                                DisplayDataInControls(((System.Windows.Controls.Grid)tb.Content));
                            }
                        }
                        else
                        {
                            DisplayDataInControls(childVisual);
                        }
                    }
                    else
                    {
                        if (childVisual.GetType() == typeof(TxtMasterData))
                        {
                            ((TxtMasterData)childVisual).LinkObjectRefresh();
                        }
                    }
                }
            }

        }
    }
}